package com.bosto.jiramanager;

import java.util.ArrayList;
import java.util.List;

import com.bosto.jiramanager.entity.Contants;
import com.bosto.jiramanager.helper.SharedPreferencesHelper;
import com.bosto.jiramanager.helper.SqlHelper;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AddDBActivity extends Activity implements OnClickListener {
	private EditText et;
	private TextView tv;
	private Button btn_add, btn_choose;
	private Spinner spinner;
	private String[] dbNamesBegin;
	private List<String> dbList;
	private ArrayAdapter<String> adapter;
	private SharedPreferencesHelper sph;
	private SqlHelper sh;
	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		context = ((ApplicationClass) getApplication()).getContext();
		setContentView(R.layout.db_layout);
		InitView();

	}

	void InitView() {
		sph = new SharedPreferencesHelper(context, Contants.DBNAMESET);
		et = (EditText) findViewById(R.id.add_db_et);
		btn_choose = (Button) findViewById(R.id.btn_choose_db);
		btn_add = (Button) findViewById(R.id.btn_add_db);
		spinner = (Spinner) findViewById(R.id.spinner_db);
		tv = (TextView) findViewById(R.id.current_db);
		tv.setText(sph.GetValue(Contants.DBCHOSEN));
		btn_add.setOnClickListener(this);
		btn_choose.setOnClickListener(this);

		dbNamesBegin = null;
		if (sph.GetValue(Contants.DBNAMESET) != null) {
			dbNamesBegin = sph.GetValue(Contants.DBNAMESET).split(",");
		}
		dbList = new ArrayList<String>();
		if ((dbNamesBegin != null) && (!dbNamesBegin[0].equals(""))) {
			for (int i = 0; i < dbNamesBegin.length; i++) {
				dbList.add(dbNamesBegin[i]);
			}
		} else {
			dbList.add(Contants.DEFAULTLIST);
		}

		adapter = new ArrayAdapter<String>(this, R.layout.custom_spinner_item,
				dbList);
		// Specify the layout to use when the list of choices appears
		adapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
		// Apply the adapter to the spinner
		spinner.setAdapter(adapter);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_add_db:
			String temp = et.getText().toString();
			sh = new SqlHelper(context, temp + Contants.DBNAMECOMMENT, null, 1);
			sh.close();
			String value = transFormat(dbNamesBegin, temp);
			sph.putValue(Contants.DBNAMESET, value);
			dbList.add(temp);
			dbNamesBegin = value.split(",");
			Toast.makeText(AddDBActivity.this, Contants.ADDDBMESSAGE,
					Toast.LENGTH_LONG).show();
			break;
		case R.id.btn_choose_db:
			String chooseDB = spinner.getAdapter()
					.getItem(spinner.getSelectedItemPosition()).toString();
			sph.putValue(Contants.DBCHOSEN, chooseDB);
			Toast.makeText(AddDBActivity.this, Contants.DBCHOSENMESSAGE,
					Toast.LENGTH_LONG).show();
			break;
		case R.id.btn_delete_db:
			String deleteDB = spinner.getAdapter()
					.getItem(spinner.getSelectedItemPosition()).toString();
			sph.putValue(Contants.DBNAMESET,
					deletetransFormat(dbNamesBegin, deleteDB));
			Toast.makeText(AddDBActivity.this, Contants.DBDELETEMESSAGE,
					Toast.LENGTH_LONG).show();
			break;
		default:
			break;

		}
	}

	String transFormat(String[] current, String value) {
		String result = "";
		if (current != null) {
			result = current[0];
			if (current.length > 1) {
				for (int i = 1; i < current.length; i++) {
					result = result + "," + current[i];
				}
			}
		} else {
			return value;
		}
		result = result + "," + value;
		return result;
	}

	String deletetransFormat(String[] current, String value) {
		String result = "";
		if (current.length == 1) {
			return result;
		}
		if (current.length >= 2) {
			int begin = 1;
			if (current[0].equals(value)) {
				result = current[1];
				begin = 2;
			} else {
				result = current[0];
			}
			if (current.length > begin) {
				for (int i = begin; i < current.length; i++) {
					if (!current[i].equals(value)) {
						result = result + "," + current[i];
					}
				}
			}
		} else {
			return result;
		}
		return result;
	}

}
